<?php
/**
 * Created by PhpStorm.
 * User: fanzhanao
 * Date: 2017/8/22
 * Time: 下午6:23
 */

namespace api\modules\v1\actions\refund;

use api\controllers\BaseRestController;

use Yii;
use yii\rest\Action;
use yii\base\Exception;
use yii\base\InvalidParamException;
use common\libraries\Log;
use api\modules\v1\models\OrderRefund;
use api\components\SearchModel;

class IndexAction extends Action
{
    public $modelClass = null;
    protected $searchModel = null;

    public function beforeRun()
    {
        $this->searchModel = new SearchModel([
            'model' => $this->modelClass,
            'defaultOrder' => ['created_at' => SORT_DESC],
            'scenario' => 'default',
            'pageSize' => Yii::$app->request->get('per-page',15)
        ]);
        return parent::beforeRun(); // TODO: Change the autogenerated stub
    }

    public function run(){
        $params = Yii::$app->request->queryParams;
        if (isset($params['name']) && $params['name']){
            if ($user = \api\models\User::findByUsername($params['name'])){
                $params['uid'] = $user->uid;
            }
            unset($params['name']);
        }
        if(isset($params['stime']) && $params['stime']){
            $params['updated_at'] ='>='.$params['stime'];
            unset($params['stime']);
        }
        if(isset($params['etime']) && $params['etime']){
            $params['updated_at'] = '<='.$params['etime'];
            unset($params['etime']);
        }
        $params['is_delete'] = 0;


        return $this->searchModel->search($params);
    }
}